package com.lgl.scs;

import com.alibaba.druid.pool.DruidDataSource;
import com.lgl.scsapi.config.DruidConfig;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@ComponentScan({"com.lgl.scs","com.lgl.scsapi"})
@ServletComponentScan({
    "com.lgl.scsapi.servlet",
    "com.lgl.scsapi.filter"
})
//开启事务管理
@EnableTransactionManagement
@EnableScheduling
//配置代理为cglib代理，默认使用 的是jdk动态代理
@EnableAsync(proxyTargetClass=true)
@SpringBootApplication
public class ScsApplication {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        DruidConfig druidConfig = getDruidConfig();
        druidDataSource.setMinEvictableIdleTimeMillis(druidConfig.getMinEvictableIdleTimeMillis());
        druidDataSource.setMaxEvictableIdleTimeMillis(druidConfig.getMaxEvictableIdleTimeMillis());
        return druidDataSource;
    }
    // 创建事务管理器
    @Bean(name = "scsTxManager")  //给事务管理器命名
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(dataSource());
    }
    // 提供SqlSeesion
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setTypeAliasesPackage("com.lgl.scsapi.system.model");
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:mybatis/**/*Mapper.xml"));
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
        Configuration configuration = sqlSessionFactory.getConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        //当字段值为空时，也返回字段
        configuration.setCallSettersOnNulls(true);
        return sqlSessionFactory;
    }
    @Bean
    public DruidConfig getDruidConfig(){
        return new DruidConfig();
    }
    @Bean
    public SqlSessionTemplate sqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryBean());
    }
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
    //使用undertow替换tomcat
    @Bean
    public ServletWebServerFactory servletContainer() {
        return new UndertowServletWebServerFactory();
    }
    public static void main(String[] args) {
        SpringApplication.run(ScsApplication.class, args);
    }

}